package edu.fiap.model.persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.fiap.util.ProjectLoggerFactory;

public class ProjectConnection {
	
	private static final Logger logger = ProjectLoggerFactory.getLogger();
	
	private static Connection connection = null;
	
	private ProjectConnection(){}
	
	public static Connection getConnection(){
		try{
			if(connection == null || connection.isClosed() == true){
				long start = System.currentTimeMillis();
				Class.forName("oracle.jdbc.driver.OracleDriver");
				//connection = DriverManager.getConnection("jdbc:oracle:thin:@oracle.fiap.com.br:1521:ORCL", "OPS$RM66033", "150487");
				connection = DriverManager.getConnection("jdbc:oracle:thin:@oracle.fiap.com.br:1521:ORCL", "OPS$RM66033", "150487");
				logger.log(Level.FINE, "Conexao com o banco de dados estabelecida em " + (System.currentTimeMillis() - start) + " ms");
				return connection;
			}else
				return connection;
		}catch(ClassNotFoundException ex){
			logger.log(Level.SEVERE,ex.getMessage(), ex);
			throw new ProjectPersistenceException(ex);
		}catch(SQLException ex){
			logger.log(Level.SEVERE,ex.getMessage(), ex);
			throw new ProjectPersistenceException(ex);
		}
	}
}